/**
 * @fileOverview resize edge
 * @author huangtonger@aliyun.com
 */
const Page = require('../../base/');
const Global = require('../global');
const Util = require('../util/');

Page.registerBehaviour('dragEdgeControlPoint', diagram => {
  const graph = diagram.getGraph();
  const rootGroup = graph.getRootGroup();
  let targetEndPoint;
  let sourceEndPoint;
  let delegation;
  let startPoint;
  let originSource;
  let originTarget;
  let originSourceAnchor;
  let originTargetAnchor;
  let edge;
  let edgeModel;
  graph.behaviourOn('edgeControlPoint:mousedown', ev => {
    if (ev.button === 2) {
      return;
    }
    const controlPoint = ev.shape;
    if (controlPoint.isTargetEndPoint()) {
      // 拖拽的是目标节点边控制点
      edge = controlPoint.getItem();
      edgeModel = edge.getModel();
      targetEndPoint = controlPoint;
      startPoint = controlPoint.getSourcePoint();
      originSource = edge.getSource();
      originSourceAnchor = edgeModel.sourceAnchor;
    } else if (controlPoint.isSourceEndPoint()) {
      // 拖拽的是源节点边控制点
      edge = controlPoint.getItem();
      edgeModel = edge.getModel();
      sourceEndPoint = controlPoint;
      startPoint = controlPoint.getTargetPoint();
      originTarget = edge.getTarget();
      originTargetAnchor = edgeModel.targetAnchor;
    }
    if (edge) {
      delegation = diagram.getDelegation([ edge ], rootGroup);
      if (originSource) {
        diagram.dragEdgeBeforeShowAnchor(originSource, originSourceAnchor, 'target');
      } else if (originTarget) {
        diagram.dragEdgeBeforeShowAnchor(originTarget, originTargetAnchor, 'source');
      }
      graph.hide(edge);
      diagram.setSignal('dragEdge', true);
    }
  });
  graph.behaviourOn('mousemove', ev => {
    if (!delegation) {
      return;
    }
    Util.dragingEdgeEndPoint({
      endPointType: originSource ? 'target' : 'source',
      edgeModel,
      graph,
      delegation,
      startPoint,
      ev,
      originSource,
      originTarget
    });
  });
  graph.behaviourOn('edgeControlPoint:mouseleave', ev => {
    if (!targetEndPoint && !sourceEndPoint) {
      if (!ev.toShape) {
        diagram.css({
          cursor: Global.cursor.beforePanCanvas
        });
      }
    }
  });
  graph.behaviourOn('mouseup', resizeEdge);
  graph.behaviourOn('canvas:mouseleave', resizeEdge);
  function resizeEdge(ev) {
    if (!delegation) {
      return;
    }
    const nodes = graph.getNodes();
    const updateModel = {};
    nodes.forEach(node => {
      diagram.clearAnchor(node);
    });
    diagram.css({
      cursor: Global.cursor.beforePanCanvas
    });
    delegation.remove();
    const isChange = Util.dropUpdateEdge({ ev, endPointType: targetEndPoint ? 'target' : 'source', model: updateModel, diagram });
    graph.show(edge);
    const edgeId = edge.id;
    if (isChange) {
      const editor = diagram.editor;
      if (editor) {
        editor.executeCommand('update', {
          itemId: edgeId,
          updateModel
        });
      } else {
        graph.update(edgeId, updateModel);
      }
    }
    resetStatus();
  }
  function resetStatus() {
    diagram.setSignal('dragEdge', false);
    targetEndPoint = undefined;
    sourceEndPoint = undefined;
    delegation = undefined;
    startPoint = undefined;
    originSource = undefined;
    originTarget = undefined;
    originSourceAnchor = undefined;
    originTargetAnchor = undefined;
    edge = undefined;
    edgeModel = undefined;
  }
}, [ 'dragHoverAnchorHotspot' ]);
